#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# Author:Summerday

import sqlite3
from matplotlib import pyplot as plt

from pyecharts.charts import Pie

from pyecharts.faker import Faker
from pyecharts import options as opts

from pyecharts.options import series_options as so
cx = sqlite3.connect("D:\\comp\\tdzl\\CBDB_20200528.db")

sql = """
select  count(*) as people_nums, c_death_age
from BIOG_MAIN where c_birthyear
    between 1368 and 1644
                 and c_death_age!=0 group by c_death_age
"""

data = cx.execute(sql)
data_list = data.fetchall()

year_list = ["1-9", "10-19", "20-29", "30-39", "40-49", "50-59", "60-69", "70-79", "80-89", "90-99", "100-"]
nums_list = []
dic = dict.fromkeys(year_list, 0)
temp = 0
n = 0
for t in data_list:
	temp = t[0]
	if len(str(t[1])) == 1:
		dic[year_list[0]] += temp
	elif len(str(t[1])) == 2:
		index = t[1] // 10
		dic[year_list[index]] += temp
	else:
		dic[year_list[10]] += n + temp

for k in dic.keys():
	nums_list.append(dic.get(k))

res = [list(z) for z in zip(year_list, nums_list)]  # 数据 [['1-9':20]]
color = ["blue", "green", "yellow", "red", "pink", "orange", "purple", "#fdae61", "#ffffbf", "#fee090", "#fdae61"]

c = (
	Pie()
		.add("", res, center=["40%", "50%"], )
		.set_colors(color)
		.set_global_opts(
		title_opts=opts.TitleOpts(title="明代平均年龄展示"),
		legend_opts=opts.LegendOpts(type_="scroll", pos_left="80%", orient="vertical"))
		.set_series_opts(label_opts=opts.LabelOpts(formatter="{b}:{c}"))
		.render("pie.html")
)
